﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tecnicas.CasoEstudio.Back.Dominio;
using Tecnicas.CasoEstudio.Repositorio;


namespace Tecnicas.CasoEstudio.Back.Persistencia
{
    public class MailDataMapper : AbstractDataMapper
    {
        public MailDataMapper()
        {             
        }
        

        public override object Insert(DomainObject obj)
        {
            if (typeof(Mail) != obj.GetType())
                throw new ApplicationException("Este mapper solo opera con objetos del tipo Mail");

            if (obj.PersistenceId == null)
            {
                obj.PersistenceId = RepositorioVersionado.Instance.GetNextKey();
                obj.PersistenceVersion = 0;
            }

            Mail mail = (Mail)obj;
            Mail mailCopia = this.ClonePlainObject(mail);

            RepositorioVersionado.Instance.Guardar(mailCopia, mailCopia.PersistenceId, mailCopia.PersistenceVersion, ++mailCopia.PersistenceVersion);

            mail.PersistenceVersion++;
            return mail;
        }
        private Mail ClonePlainObject(Mail mail)
        {
            Mail mailCopia = new Mail();
            mailCopia.PersistenceId = mail.PersistenceId;
            mailCopia.PersistenceVersion = mail.PersistenceVersion;

            mailCopia.Direccion = mail.Direccion;
            return mailCopia;
        }
        public override void Delete(long? id, object version)
        {
            RepositorioVersionado.Instance.Eliminar(typeof(Mail), id, version);
        }
        public override void Delete(DomainObject obj)
        {
            this.Delete(obj.PersistenceId, obj.PersistenceVersion);
        }

        public override DomainObject Find(long? id)
        {
            return (Mail)RepositorioVersionado.Instance.ObtenerPorId(typeof(Mail), id);
        }

    }
}
